package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.*;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service

public class ClazzServiceImpl implements ClazzService {
    //业务层接口实现类
    @Autowired
    private ClazzMapper clazzMapper;


    @Override
    public void deleteById(Integer id) {
        clazzMapper.deleteById(id);
    }

    @Override
    public void add(Clazz clazz) {
        //补全参数
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.add(clazz);
    }

    @Override
    public Clazz findById(Integer id) {
       return clazzMapper.findById(id);
    }

    @Override
    public void update(Clazz clazz) {
        //补全参数
        clazz.setUpdateTime(LocalDateTime.now());
         clazzMapper.update(clazz);
    }

    @Override
    public List<Clazz> findAll() {
        return clazzMapper.findAll();
    }

    @Override
    public PageResult<Clazz> page(ClazzQueryParm clazzQueryParm) {

        //1 设置分页参数
        PageHelper.startPage(clazzQueryParm.getPage(), clazzQueryParm.getPageSize());
        //2 执行查询
       List<Clazz> clazzList = clazzMapper.page(clazzQueryParm);
        //3 解析结果封装PageResult对象并返回
        Page<Clazz> p = (Page<Clazz>) clazzList;


      return new PageResult<Clazz>(p.getTotal(),p.getResult());
    }


}
